package com.shine.icis.util;

import java.io.IOException;
import java.util.Timer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shine.cotrun.web.comm.XMLResponse;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.cotrun.web.upload.UploadConfig;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysRuntimeParams;

/**
 * @author zsy
 */
public class ServletListener implements ServletContextListener {

	/**
	 * <code>serialVersionUID</code> 的注释
	 */
	private static final long serialVersionUID = 2849931988267060670L;

	private static final Log log = LogFactory.getLog(ServletListener.class);
	private ServletContext context;
	
	private Timer timer;// 定时器
	private int timerInterval = 3600000; // 定时周期一个小时

	/*
	 * （非 Javadoc）
	 * 
	 * @see javax.servlet.ServletContextListener
	 *      #contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent event) {
		context = event.getServletContext();
		log.info("ServletContex start...");
		// 系统配置文件载入
		initXMLConfig();

		// 初始化数据字典表
		 initDBConfig();

		timer = new Timer(true);
		timer.schedule(new PeriodTask(), timerInterval, timerInterval);
	}
	
	public void init(ServletConfig config) throws ServletException {
		context = config.getServletContext();
	}

	/*
	 * （非 Javadoc）
	 * 
	 * @see javax.servlet.ServletContextListener
	 *      #contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent event) {
		timer.cancel();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("act");
		if ("reoadxml".equals(action)) {
			if (initXMLConfig()) {
				XMLResponse.outputStandardResponse(response, 1, "热加载成功");
			} else {
				XMLResponse.outputStandardResponse(response, 0, "热加载失败");
			}
		} else if ("redeploy".equals(action)) {
			if (initDBConfig()) {
				XMLResponse.outputStandardResponse(response, 1, "热加载成功");
			} else {
				XMLResponse.outputStandardResponse(response, 0, "热加载失败");
			}
		}
	}

	private boolean initDBConfig() {
		boolean result = false;
		if (SysCodeDictLoader.getInstance().initAllCodeDict()) {
			log.info("初始化数据字典表...OK");
			result = true;
		} else {
			log.error("初始化数据字典表...失败！");
		}
		return result;
	}

	private boolean initXMLConfig() {
		boolean result = false;
		SysRuntimeParams srtp = SysRuntimeParams.loadSysRuntimeParams();
		if (srtp != null) {
			log.info("装载系统配置文件...OK");
			result = true;
		} else {
			log.error("装载系统配置文件...失败！");
			return result;
		}
		//上传图片的路径
		UploadConfig config = srtp.getUploadConfig();
		String basePath = context.getRealPath("/");
		config.setBasePath(basePath);
		FileDispatcher.setConfig(config);
		return result;
	}

}
